const express = require('express');
const cors = require('cors');
const path = require('path');

const app = express();
const PORT = process.env.PORT || 3000;

// 中间件
app.use(cors());
app.use(express.json());
app.use(express.static('public'));

// 兔子图片URL列表
const rabbitImages = [
  'https://images.unsplash.com/photo-1585110396000-c9ffd4e4b308?w=500&h=500&fit=crop',
  'https://images.unsplash.com/photo-1591561582301-7ce6588cc286?w=500&h=500&fit=crop',
  'https://images.unsplash.com/photo-1452857297128-d9c29ecba191?w=500&h=500&fit=crop',
  'https://images.unsplash.com/photo-1425082661705-1834bfd09dca?w=500&h=500&fit=crop',
  'https://images.unsplash.com/photo-1518288777172-921b91b670ec?w=500&h=500&fit=crop',
  'https://images.unsplash.com/photo-1459664018906-085c36f472af?w=500&h=500&fit=crop',
  'https://images.unsplash.com/photo-1518791841217-8f162f1e1131?w=500&h=500&fit=crop',
  'https://images.unsplash.com/photo-1511044568932-338cba0ad803?w=500&h=500&fit=crop',
  'https://images.unsplash.com/photo-1544029591-2d672595d308?w=500&h=500&fit=crop',
  'https://images.unsplash.com/photo-1518288777172-921b91b670ec?w=500&h=500&fit=crop'
];

// API路由：获取随机兔子图片
app.get('/api/random-rabbit', (req, res) => {
  const randomIndex = Math.floor(Math.random() * rabbitImages.length);
  const randomImage = rabbitImages[randomIndex];
  
  res.json({
    success: true,
    imageUrl: randomImage,
    timestamp: new Date().toISOString()
  });
});

// 健康检查路由
app.get('/api/health', (req, res) => {
  res.json({ status: 'OK', message: 'Rabbit generator is running!' });
});

// 首页路由
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

// 启动服务器
app.listen(PORT, () => {
  console.log(`🐰 兔子照片生成器运行在 http://localhost:${PORT}`);
  console.log(`📸 访问 http://localhost:${PORT}/api/random-rabbit 获取随机兔子图片`);
});
